<?php

class UserController extends Silk_Controller_Admin_Action {

    protected $_norefer = array(
	'create', 'edit', 'delete', 'changepassword'
    );

    public function init(){
	parent::init();
	$this->appendBreadcrumbs(Silk::Translate('Manage Users'), $this->resolveRoute('user_admin'));
    }

    public function adminAction(){

	$this->initPaginatorParams('silk_cms_users___', 'username');

	$session = Silk::getSession();

	if ($this->_request->has('criteria'))
	    $session->criteria = $this->_getParam('criteria');
	else
	    $session->criteria = null;

	$Users = new Silk_Users();

	$paginator = $Users->getPaginator($session->criteria, $session->offset, $session->limit, "{$session->sort} {$session->order}");

	$paginator->setItemCountPerPage($session->limit);
	$paginator->setCurrentPageNumber($session->page);

	$this->view->users = $paginator;

    }

    public function createAction(){
	$this->appendBreadcrumbs(Silk::Translate('Create User'));
	$Users = new Silk_Users();

	if ($this->_request->isPost()){

	    if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('user_admin');

	    try {

		$user = $Users->create($this->_getParam('user'));
		$user->save();
		$this->gotoRoute('user_admin');

	    } catch (FP_Validation_Exception $x){

		foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
	    }

	} else
	    $user = $Users->create();

	$this->view->user = $user;
    }

    public function editAction(){
	$this->appendBreadcrumbs(Silk::Translate('Edit User'));
	$Users = new Silk_Users();
	$user  = $Users->fetch($this->_getParam('user_id'));

	if ($user){
	    if ($this->_request->isPost()){

		if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('user_admin');

		try {

		    $user->import($this->_getParam('user'));
		    $user->save();
		    $this->gotoRoute('user_admin');

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->user = $user;
	} else
	    throw new FP_Route_Exception('Invalid user id', FP_Route_Exception::NO_RESOURCE);
    }

    public function deleteAction(){
	$this->appendBreadcrumbs(Silk::Translate('Delete User'));
	$Users = new Silk_Users();
	$user  = $Users->fetch($this->_getParam('user_id'));

	if ($user){
	    if ($this->_request->isPost()){

		if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('user_admin');

		try {

		    $user->delete();
		    $this->gotoRoute('user_admin');

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->user = $user;
	} else
	    throw new FP_Route_Exception('Invalid user id', FP_Route_Exception::NO_RESOURCE);
    }

    public function changepasswordAction(){
	$this->appendBreadcrumbs(Silk::Translate('Change Password'));
	$Users = new Silk_Users();
	$user  = $Users->fetch($this->_getParam('user_id'));

	if ($user){
	    if ($this->_request->isPost()){

		if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('user_admin');

		try {

		    if ($this->_getParam('password') == $this->_getParam('confirm_password')){

			$user->password = Silk::EncryptPassword($this->_getParam('password'));
			$user->save();
			$this->gotoRoute('user_admin');
		    } else
			throw new FP_Validation_Exception('Passwords do not match', null, array('Password' => array(0 => 'Passwords do not match')));

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->user = $user;
	} else
	    throw new FP_Route_Exception('Invalid user id', FP_Route_Exception::NO_RESOURCE);
    }

}

?>